/*******************************************************************************
 * Copyright (c) 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 ******************************************************************************/
package net.sourceforge.perforce.ui.internal.dnd;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import net.sourceforge.perforce.core.api.IPerforceServer;
import net.sourceforge.perforce.core.resources.IP4File;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.dnd.DragSourceAdapter;
import org.eclipse.swt.dnd.DragSourceEvent;

/**
  * @version $Revision: 169 $
  * @author  <a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class PendingChangelistDragAdapter extends DragSourceAdapter 
{
	// --------------------------------------------------------------------------- Private Variables
	
	/** Stores the selection provider. */
	private ISelectionProvider selectionProvider;
	
	// ------------------------------------------------------------------------- Public Constructors
	
	/** Constructor for PendingChangelistDragAdapter.
	  *
	  * @param provider the selection provider 
	  */ 
	public PendingChangelistDragAdapter(ISelectionProvider provider) 
	{
		selectionProvider = provider;
	}
	
	// ------------------------------------------------------------------------------ Public Methods
	
	/** @see DragSourceAdapter#dragSetData(DragSourceEvent) */
	public void dragSetData(DragSourceEvent event) 
	{
		if (LocalFileTransfer.getInstance().isSupportedType(event.dataType))
		{
			event.data = getSelectedLocalFiles();
		}
	}

	/** @see DragSourceAdapter#dragStart(DragSourceEvent) */
	public void dragStart(DragSourceEvent event) 
	{
		Set serverSet = new HashSet();	
		IStructuredSelection selection = (IStructuredSelection) selectionProvider.getSelection();
		for (Iterator it = selection.iterator(); it.hasNext();) 
		{
			Object next = it.next();
			if (!(next instanceof IP4File))
			{
				event.doit = false;
				return;
			}
			else
			{
				IP4File file = (IP4File) next;
				IPerforceServer server = file.getServer();
				if (!file.getClientName().equals(server.getClient()))
				{
					event.doit = false;
					return;
				}
				 
				serverSet.add(server);
			}
		}
		
		event.doit = serverSet.size() == 1;
	}
	
	// ----------------------------------------------------------------------------- Private Methods

	/** Returns the currently selected LocalFiles.
	  * 
	  * @return array of selected LocalFiles.
	  */
	private Object getSelectedLocalFiles() 
	{
		IStructuredSelection selection = (IStructuredSelection) selectionProvider.getSelection();
		if (selection == null || selection.isEmpty())
		{ 
			return null;
		}
		
		List result = new ArrayList();
		for (Iterator it = selection.iterator(); it.hasNext();)
		{
			Object next = it.next();
			if (next instanceof IP4File)
			{
				result.add(next);
			}
		}
				
		return (IP4File[]) result.toArray(new IP4File[result.size()]);
	}
}
